/**
 * Created by huachuan on 2016/1/6.
 */
//获取本周的时间所有时间
function getWeekDay(nowDate) {
  var now = nowDate;
  var nowDayOfWeek = now.getDay();         //今天本周的第几天
  var nowDay = now.getDate();              //当前日
  var nowMonth = now.getMonth();           //当前月
  var nowYear = now.getYear();
  var weekDay = [];
  nowYear += (nowYear < 2000) ? 1900 : 0;//当前年
  for (var i = nowDay - nowDayOfWeek; i <= nowDay + (6 - nowDayOfWeek); i++) {
    var dayShow = {
      time: '',
      timeformat: '',
      nong_time:'',
      selected: false
    };
    dayShow.time = new Date(nowYear, nowMonth, i);
    dayShow.nong_time = getLunarDate(dayShow.time);
    dayShow.timeformat = formatDate(new Date(nowYear, nowMonth, i));
    if (formatDate(new Date(nowYear, nowMonth, i)) == formatDate(now)) {//如果是当天的日期
      dayShow.selected = true;
    }
    weekDay.push(dayShow);
  }
  return weekDay;
}

//格式化时间
function formatDate(date) {
  var nowYear = date.getYear();
  nowYear += (nowYear < 2000) ? 1900 : 0;//当前年
  var mymonth = date.getMonth() + 1;
  var myweekday = date.getDate();
  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  return (nowYear + "-" + mymonth + "-" + myweekday);
}
function formatDateDetail(now) {
  var nowYear = now.getYear();
  nowYear += (nowYear < 2000) ? 1900 : 0;//当前年
  var mymonth = now.getMonth() + 1;
  var myweekday = now.getDate();
  if (mymonth < 10) {
    mymonth = "0" + mymonth;
  }
  if (myweekday < 10) {
    myweekday = "0" + myweekday;
  }
  var hour=now.getHours();
  var minute=now.getMinutes();
  var second=now.getSeconds();
  return nowYear+"-"+mymonth+"-"+myweekday+" "+hour+":"+minute+":"+second;
}
//去除数组的重复项
function unique(arr) {
  var result = [], isRepeated;
  for (var i = 0, len1 = arr.length; i < len1; i++) {
    isRepeated = false;
    for (var j = 0, len2 = result.length; j < len2; j++) {
      if (arr[i].member_id == result[j].member_id) {
        isRepeated = true;
        break;
      }
    }
    if (!isRepeated) {
      result.push(arr[i]);
    }
  }
  return result;
}
function uniqueWine(arr) {
  var result = [], isRepeated;
  for (var i = 0, len1 = arr.length; i < len1; i++) {
    isRepeated = false;
    for (var j = 0, len2 = result.length; j < len2; j++) {
      if (arr[i].wine_id == result[j].wine_id) {
        isRepeated = true;
        break;
      }
    }
    if (!isRepeated) {
      result.push(arr[i]);
    }
  }
  return result;
}
//获取图片的原始尺寸
function getNaturalWidth(imgSrc) {
  var image = new Image();
  image.src = imgSrc;
  var naturalWidth = image.width;
  var naturalHeight = image.height;
  return [naturalWidth,naturalHeight];
}

//处理图片，先按传入的宽高进行等比例放缩，再将超出的部分隐藏
function handleImg(width,height) {
  var classElements = document.getElementsByClassName("change_img");
  for(var i =0;i<classElements.length;i++){
    var img = new Image();
    img.src = classElements[i].src;//获取图片路径
    var scale=Math.max(width/img.width, height/img.height);
    var newWidth = img.width*scale;
    var newHeight = img.height*scale;
    classElements[i].style.height = newHeight+"px";
    classElements[i].style.width = newWidth+"px";
    if(newHeight > height){
      //高做截取
      classElements[i].style.marginTop=(height-newHeight)/2+"px";
      classElements[i].style.marginBottom=(height-newHeight)/2+"px";
    }else if(newWidth > width){
      //宽做截取
      classElements[i].style.marginLeft=(width-newWidth)/2+"px";
      classElements[i].style.marginRight=(width-newWidth)/2+"px";
    }
  }
}

function formatNum(num) {
  num = num.toString();   //将输入的数字转换为字符串

  if(/^-?\d+\.?\d+$/.test(num)){  //判断输入内容是否为整数或小数
    if(/^-?\d+$/.test(num)){    //判断输入内容是否为整数
      num =num + ",00";   //将整数转为精度为2的小数，并将小数点换成逗号
    }else{
      num = num.replace(/\./,',');    //将小数的小数点换成逗号
    }

    while(/\d{4}/.test(num)){ //
      /***
       *判断是否有4个相连的数字，如果有则需要继续拆分，否则结束循环；
       *将4个相连以上的数字分成两组，第一组$1是前面所有的数字（负数则有符号），
       *第二组第一个逗号及其前面3个相连的数字；
       * 将第二组内容替换为“,3个相连的数字，”
       ***/
      num = num.replace(/(\d+)(\d{3}\,)/,'$1,$2');
    }

    num = num.replace(/\,(\d*)$/,'.$1');   //将最后一个逗号换成小数点
    return num.substring(0,num.length-3);
  }else{
    return num;
  }
}
//uuid处理
function uuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  s[8] = s[13] = s[18] = s[23] = "-";

  return s.join("");
}
